﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using UnityEditor;
using UnityEngine;

namespace UPA
{
    public partial class ProcessActionGroupDrawer
    {
        private string[] _canAddActionNames;
        private List<Type> _canAddActionTypes;
        private int _selectAddActionIndex = 0;
        private Vector2 _templateScroll;

        private void RefreshTemplate()
        {
            _selectAddActionIndex = 0;
            _canAddActionTypes = AppDomain.CurrentDomain.GetAssemblies()
                .SelectMany(assembly => assembly.GetTypes())
                .Where(t => t.IsClass && !t.IsAbstract && typeof(ProcessAction).IsAssignableFrom(t) &&
                            (t.GetCustomAttribute<ProcessActionAllowMultipleAttribute>() != null || _group.ActionUnits.All(unit => unit.ActionType != t.FullName))).ToList();
            _canAddActionNames = new string[_canAddActionTypes.Count];
            int index = 0;
            foreach (var type in _canAddActionTypes)
            {
                var actionName = type.Name;
                var attribute = type.GetCustomAttribute<ProcessActionTemplateNameAttribute>();
                if (attribute != null && !string.IsNullOrWhiteSpace(attribute.Name))
                {
                    actionName = attribute.Name;
                }

                _canAddActionNames[index] = actionName;
                index++;
            }
        }

        private void DrawTemplate()
        {
            _scrollValue = EditorGUILayout.BeginScrollView(_scrollValue);
            DrawCurActions();
            EditorGUILayout.EndScrollView();
            GUILayout.FlexibleSpace();
            DrawAddNewAction();
            DrawSaveTemplate();
        }

        private void DrawCurActions()
        {
            int removeIndex = -1;
            for (int i = 0; i < _group.ActionUnits.Count; i++)
            {
                var unit = _group.ActionUnits[i];
                EditorGUILayout.BeginHorizontal();
                var originColor = GUI.color;
                if (unit.Action == null)
                {
                    GUI.color = Color.red;
                }
                
                GUILayout.Label(unit.Name, _textFieldStyle);
                GUI.color = originColor;
                
                if (i > 0)
                {
                    if (GUILayout.Button("上移", _btnStyle))
                    {
                        _group.SwapAction(i-1, i);
                    }
                }
                else
                {
                    _tmpContent.text = "上移";
                    GUILayout.Space(_btnStyle.CalcSize(_tmpContent).x);
                }

                if (i < _group.ActionUnits.Count - 1)
                {
                    if (GUILayout.Button("下移", _btnStyle))
                    {
                        _group.SwapAction(i, i+1);
                    }
                }
                else
                {
                    _tmpContent.text = "下移";
                    GUILayout.Space(_btnStyle.CalcSize(_tmpContent).x);
                }

                if (GUILayout.Button("删除", _btnStyle))
                {
                    removeIndex = i;
                }

                EditorGUILayout.EndHorizontal();
            }

            if (removeIndex >= 0)
            {
                _group.RemoveAction(removeIndex);
                RefreshTemplate();
            }
        }

        private void DrawAddNewAction()
        {
            if (_canAddActionTypes == null || _canAddActionTypes.Count == 0)
            {
                return;
            }

            EditorGUILayout.BeginHorizontal();
            _selectAddActionIndex = EditorGUILayout.Popup(_selectAddActionIndex, _canAddActionNames,_popupStyle);
            if (GUILayout.Button("添加", _btnStyle))
            {
                _group.AddAction(_canAddActionTypes[_selectAddActionIndex]);
                RefreshTemplate();
            }

            EditorGUILayout.EndHorizontal();
        }

        private void DrawSaveTemplate()
        {
            if (GUILayout.Button("保存",_btnStyle,GUILayout.ExpandWidth(true),GUILayout.Height(50)))
            {
                AssetDatabase.SaveAssetIfDirty(serializedObject.targetObject);
            }
        }
    }
}